package com.lt.lumentracks.controller;


import com.aliyuncs.exceptions.ClientException;
import com.lt.lumentracks.annotation.LogIp;
import com.lt.lumentracks.entity.dto.user.UserAddDTO;
import com.lt.lumentracks.entity.dto.user.UserDTO;
import com.lt.lumentracks.entity.dto.user.UserLoginDTO;
import com.lt.lumentracks.entity.dto.user.UserUpdateDTO;
import com.lt.lumentracks.entity.vo.common.PageVO;
import com.lt.lumentracks.entity.vo.common.Result;
import com.lt.lumentracks.entity.po.User;
import com.lt.lumentracks.entity.vo.user.UserVO_conversationList;
import com.lt.lumentracks.entity.vo.user.UserVO_info;
import com.lt.lumentracks.entity.vo.user.UserVO_post;
import com.lt.lumentracks.entity.vo.user.UserVO_profile;
import com.lt.lumentracks.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户基础信息表 前端控制器
 * </p>
 *
 * @author swl
 * @since 2025-07-07
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;


    // 增
    @PostMapping("/add")
    public Result<Integer> add(@RequestBody UserAddDTO addDTO) {
        return userService.addUser(addDTO);
    }


    // 删
    @DeleteMapping("/delete")
    public Result<String> delete(@RequestParam Integer id) {
        return userService.delUserById(id);
    }


    // 改--修改个人资料
    @PutMapping("/update")
    public Result<String> update(@RequestBody UserUpdateDTO updateDTO) {
        return userService.updateUserById(updateDTO);
    }

    // 改--修改个人头像
    @PutMapping("/updateAvatar")
    public Result<String> updateAvatar(@RequestParam Integer uid, @RequestParam String avatarUrl) throws ClientException {
        return userService.updateAvatar(uid, avatarUrl);
    }


    // 查
    // 查全部
    @GetMapping("/getAll")
    public Result<List<User>> getAll() {
        return userService.getAllUser();
    }

    // 分页查全部
    @GetMapping("/getAllByPage")
    public Result<PageVO<User>> getAllByPage(@RequestParam(defaultValue = "1") long current,
                                             @RequestParam(defaultValue = "10") long size) {
        PageVO<User> allInPage = userService.getAllUserByPage(current, size);
        return Result.success(allInPage);
    }

    // 条件查询
    @PostMapping("/getByCondition")
    public Result<PageVO<User>> getByCondition(@RequestBody UserDTO userDTO) {
        PageVO<User> allInCondition = userService.getInCondition(userDTO);
        return Result.success(allInCondition);
    }

    // 根据id查询用户--返回部分信息（其他用户个人主页查询业务用）
    @GetMapping("/getById_ProfileQuery")
    public Result<UserVO_profile> getById_ProfileQuery(@RequestParam Integer id) {
        UserVO_profile vo = userService.getById_ProfileQuery(id);
        return Result.success(vo);
    }

    // 根据id查询用户--返回部分信息（查询帖子业务用）
    @GetMapping("/getById_PostQuery")
    public Result<UserVO_post> getById_PostQuery(@RequestParam Integer id) {
        UserVO_post vo = userService.getById_PostQuery(id);
        return Result.success(vo);
    }

    // 根据id批量查询用户--返回部分信息（查询会话列表用）
    @PostMapping("/getById_ConvListQuery")
    public Result<List<UserVO_conversationList>> getById_ConvListQuery(@RequestBody List<Integer> ids) {
        if (ids == null || ids.isEmpty()){
            return Result.error("参数错误");
        }
        List<UserVO_conversationList> vo = userService.getById_ConvListQuery(ids);
        return Result.success(vo);
    }



    // 用户登录
    @PostMapping("/login")
    public Result<String> login(@RequestBody @Validated UserLoginDTO userLoginDTO) {
        return userService.login(userLoginDTO);
    }

    // 用户注册
    @PostMapping("/register")
    public Result<Integer> register(@RequestBody @Validated UserAddDTO userAddDTO) {
        return userService.addUser(userAddDTO);
    }



    // 根据jwt令牌返回用户具体信息
    @PostMapping("/getUserInfoByJwt")
    @LogIp
    public Result<UserVO_info> getUserInfoByJwt(@RequestParam String jwt) {
        UserVO_info vo = userService.getUserInfoByJwt(jwt);
        if(vo == null){
            return Result.error("NOT_LOGIN");
        }
        return Result.success(vo);
    }

}
